package com.leisure.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

/**
 * ClassName: JWTUtil
 * Package: com.leisure.utils
 * Description: This class is used for creating JWT and
 * verifying JWT
 *
 * @Author: MyLeisureLife
 * @Date: 2024/6/8:15:49:54 星期六
 */
public class JWTUtil {
    private static final String KEY = "leisure";

    /**
     * create JWT token
     * @param claims payload data
     * @return JWT token
     */
    public static String genJWT(Map<String, Object> claims){
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000*60*60*12))
                .sign(Algorithm.HMAC256(KEY));
    }

    /**
     * parse JWT Token
     * @param jwtToken token
     * @return payload data
     */
    public static Map<String, Object> parseJWT(String jwtToken){
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(jwtToken)
                .getClaim("claims")
                .asMap();
    }
}
